package com.etc.service.impl;

import com.etc.Dao.UserDao;
import com.etc.entity.Score;
import com.etc.entity.Student;
import com.etc.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.util.List;

public class Userservice implements UserDao {

    private SqlSessionFactory sqlSessionFactory;

    public Userservice(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }




    @Override
    public User findById(User userid) {
        return null;
    }

    @Override
    public User findById(Integer userid) {
        SqlSession sqlSession =null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息
            User user = sqlSession.selectOne("findById", userid);
            return  user;
            // 输出用户信息

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

    }



    @Override
    public List<User> findbyall() {

        SqlSession sqlSession =null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息
            List<User> findbyall = sqlSession.selectList("findbyall");
            return findbyall;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

    }


    //添加
    @Override
    public Integer save(User user) {
        SqlSession sqlSession =null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息
            int save = sqlSession.insert("save", user);
            return save;
            // 输出用户信息
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }

    }

    @Override
    public Integer Update(User user) {

        SqlSession sqlSession =null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息
            int i=sqlSession.update("Update",user);
//            提交
            return i;
            // 输出用户信息
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return null;
    }



    @Override
    public Integer deleteId(Integer userid) {
        SqlSession sqlSession =null;
        try {
            // 创建数据库会话实例sqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询单个记录，根据用户id查询用户信息
            User user = new User();
            user.setId(1);
            int i=sqlSession.delete("deleteId",userid);
            return i;
            // 输出用户信息
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public List<User> order() {
        SqlSession sqlSession =null;
        sqlSession = sqlSessionFactory.openSession();
        List<User> order = sqlSession.selectList("order");

        return order;
    }

    @Override
    public List<User>  between() {
         SqlSession sqlSession =null;
        sqlSession = sqlSessionFactory.openSession();
        List<User>between = sqlSession.selectList(" between");
        return between;
    }

    @Override
    public  List<Score> in(){
        SqlSession sqlSession =null;
//        但是如果使用的是openSession方法创建的session的话，那么必须显示的关闭session，也就是调用session.close()方法
        sqlSession =sqlSessionFactory.openSession();
        List<Score>in=sqlSession.selectList("in");
        return  in;
    }

    @Override
    public List<Student> or() {
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        List<Student>or=sqlSession.selectList("or");
      return  or;
    }
    @Override
    public List<Student>orderby(){
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        List<Student>orderby=sqlSession.selectList("orderby");
        return  orderby;
    }
    @Override
    public List<Score>asc(){
        SqlSession sqlSession=null;
        sqlSession=sqlSessionFactory.openSession();
        List<Score> asc = sqlSession.selectList("asc");
        return  asc;
    }

    @Override
    public Student count() {
        SqlSession sqlSession =null;
        try {
            SqlSession sqlSession1 = sqlSessionFactory.openSession();
            Student count = sqlSession1.selectOne("count");
            return  count;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Override
    public List<Score> avg() {
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        List<Score>avg =sqlSession.selectList("avg");
        return avg;

    }
    @Override
    public  Score mh(){
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        Score mh=sqlSession.selectOne("mh");
        return mh;
    }
    @Override
    public List<Score>degree(){
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        List<Score> degree = sqlSession.selectList("degree");
        return degree;

    }

    @Override
    public List<Student> mhc(String Sname) {
        SqlSession sqlSession =null;
        sqlSession=sqlSessionFactory.openSession();
        List<Student> mhc = sqlSession.selectList("mhc", Sname);
        return mhc;
    }




}









